package com.zhoug.zmui3demo.ui;

import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zhoug.common3.interfaces.TextChangedListener;
import com.zhoug.common3.utils.LogUtils;
import com.zhoug.common3.utils.ViewUtils;
import com.zhoug.zmui3.autocomplete.OnItemClickListener;
import com.zhoug.zmui3.autocomplete.ZMUIAutoCompleteAdapter;
import com.zhoug.zmui3.autocomplete.ZMUIAutoCompleteFilter;
import com.zhoug.zmui3.autocomplete.ZMUIAutoCompleteSimpleAdapter;
import com.zhoug.zmui3.autocomplete.ZMUIAutoCompleteTextView;
import com.zhoug.zmui3.autocomplete.ZMUIMultiAutoCompleteTextView;
import com.zhoug.zmui3.input.ZMUIInputAutoCompleteTextView;
import com.zhoug.zmui3.input.ZMUIInputMultiAutoCompleteTextView;
import com.zhoug.zmui3.input.ZMUIInputView;
import com.zhoug.zmui3demo.R;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SwitchCompat;
import androidx.lifecycle.LifecycleOwner;
import io.reactivex.Observable;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.disposables.Disposable;


/**
 * 输入自动补全
 *
 * @Author: zhoug
 * @Date: 2024-07-22
 * @Description:
 */
public class TestZMUIAutoCompleteTextActivity extends AppCompatActivity {
    private static final String TAG = ">>>TestZMUIAutoCompleteTex";
    private SwitchCompat switchError;
    private SwitchCompat switchEnable;
    private ZMUIMultiAutoCompleteTextView autoCompleteCity;
    private ZMUIAutoCompleteTextView autoCompleteDept;
    private ZMUIInputAutoCompleteTextView InputAutoCompleteTextView;
    private ZMUIInputMultiAutoCompleteTextView InputMultiAutoCompleteTextView;
    private EditText editTextAutoComplete;
    private Button btnSubmit;
    private TextView tvResult;
    private LinearLayout root;

    protected ViewGroup getRoot() {
        return root;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_auto_complete);
        findViews();
        switchEnable.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                ViewUtils.findView(getRoot(), ZMUIInputView.class, true, view -> {
                    view.setEnabled(!isChecked);
                });
            }
        });

        switchError.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                ViewUtils.findView(getRoot(), ZMUIInputView.class, true, view -> {
                    view.setError(isChecked);
                });
            }
        });
        ZMUIAutoCompleteSimpleAdapter<City> cityZMUIAutoCompleteSimpleAdapter = new ZMUIAutoCompleteSimpleAdapter<>();
        cityZMUIAutoCompleteSimpleAdapter.setFilter(new CityFilter(this, cityZMUIAutoCompleteSimpleAdapter));
        autoCompleteCity.setAdapter(cityZMUIAutoCompleteSimpleAdapter);

        autoCompleteCity.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id, Object itemData) {
                LogUtils.d(TAG, "autoCompleteCity onItemClick:" + itemData);
            }
        });
        ZMUIAutoCompleteSimpleAdapter<Dept> deptZMUIAutoCompleteSimpleAdapter = new ZMUIAutoCompleteSimpleAdapter<>();
        deptZMUIAutoCompleteSimpleAdapter.setFilter(new DeptFilter(this, deptZMUIAutoCompleteSimpleAdapter));
        autoCompleteDept.setAdapter(deptZMUIAutoCompleteSimpleAdapter);
        autoCompleteDept.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id, Object itemData) {
                LogUtils.d(TAG, "autoCompleteDept onItemClick:" + itemData);

            }
        });

        cityZMUIAutoCompleteSimpleAdapter = new ZMUIAutoCompleteSimpleAdapter<>();
        cityZMUIAutoCompleteSimpleAdapter.setFilter(new CityFilter(this, cityZMUIAutoCompleteSimpleAdapter));
        InputAutoCompleteTextView.setAdapter(cityZMUIAutoCompleteSimpleAdapter);
        InputAutoCompleteTextView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id, Object itemData) {
                LogUtils.d(TAG, "InputAutoCompleteTextView onItemClick:" + itemData);
            }
        });

        cityZMUIAutoCompleteSimpleAdapter = new ZMUIAutoCompleteSimpleAdapter<>();
        cityZMUIAutoCompleteSimpleAdapter.setFilter(new CityFilter(this, cityZMUIAutoCompleteSimpleAdapter));
        InputMultiAutoCompleteTextView.setAdapter(cityZMUIAutoCompleteSimpleAdapter);
        InputMultiAutoCompleteTextView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id, Object itemData) {
                LogUtils.d(TAG, "InputAutoCompleteTextView onItemClick:" + itemData);
            }
        });

        editTextAutoComplete.addTextChangedListener(new TextChangedListener() {
            @Override
            public void afterTextChanged(Editable s) {

            }
        });


        btnSubmit.setOnClickListener(v -> {
            String result = "城市:";
            Editable text = autoCompleteCity.getText();
            if (text != null) {
                result += text.toString().trim() + "\n";
            } else {
                result += "\n";
            }
            result += "部门:";
            text = autoCompleteDept.getText();
            if (text != null) {
                result += text.toString().trim() + "\n";
            } else {
                result += "\n";
            }
            result += "单选:";
            String value = InputAutoCompleteTextView.getValue();
            if (value != null) {
                result += value.trim() + "\n";
            } else {
                result += "\n";
            }
            result += "多选:";
            value = InputMultiAutoCompleteTextView.getValue();
            if (value != null) {
                result += value.trim() + "\n";
            } else {
                result += "\n";
            }
            tvResult.setText(result);
        });
    }

    private void findViews() {
        switchError = findViewById(R.id.switch_error);
        switchEnable = findViewById(R.id.switch_enable);
        autoCompleteCity = findViewById(R.id.autoCompleteCity);
        autoCompleteDept = findViewById(R.id.autoCompleteDept);
        InputAutoCompleteTextView = findViewById(R.id.InputAutoCompleteTextView);
        InputMultiAutoCompleteTextView = findViewById(R.id.InputMultiAutoCompleteTextView);
        editTextAutoComplete = findViewById(R.id.editTextAutoComplete);
        btnSubmit = findViewById(R.id.btn_submit);
        tvResult = findViewById(R.id.tv_result);
        root = findViewById(R.id.root);
    }


    private static final class City {
        private String name;
        private String code;

        public City(String name, String code) {
            this.name = name;
            this.code = code;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getCode() {
            return code;
        }

        public void setCode(String code) {
            this.code = code;
        }

        @Override
        public String toString() {
            return "City{" +
                    "name='" + name + '\'' +
                    '}';
        }
    }


    public static final class Dept {
        private String id;
        private String name;

        public Dept(String id, String name) {
            this.id = id;
            this.name = name;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        @Override
        public String toString() {
            return "Dept{" +
                    "name='" + name + '\'' +
                    '}';
        }
    }


    private static final class DeptFilter extends ZMUIAutoCompleteFilter<Dept> {
        private Disposable mDisposable;

        public DeptFilter(LifecycleOwner lifecycleOwner, ZMUIAutoCompleteAdapter<Dept> adapter) {
            super(lifecycleOwner, adapter);
        }

        @Override
        public String converterString(Dept city) {
            return city.getName();
        }

        @Override
        protected void loadListDataSyn(@Nullable String filter, @NonNull List<Dept> Result) {
            mDisposable = Observable
                    .create((ObservableOnSubscribe<List<Dept>>) emitter -> {
                        List<Dept> depts = new ArrayList<>();
                        if (filter == null || filter.isEmpty()) {
                            depts.addAll(DEPT_DATA_SET);
                        } else {
                            for (Dept item : DEPT_DATA_SET) {
                                if (item.getName().contains(filter)) {
                                    depts.add(item);
                                }
                            }
                        }
                        emitter.onNext(depts);
                        emitter.onComplete();

                    })
                    .subscribe(list -> {
                        if (list != null && !list.isEmpty()) {
                            Result.addAll(list);
                        }
                    }, throwable -> {
                        throwable.printStackTrace();

                    });


        }

        @Override
        protected void cancelLoadList() {
            if (mDisposable != null && !mDisposable.isDisposed()) {
                mDisposable.dispose();
                mDisposable = null;
            }
        }
    }

    private static final class CityFilter extends ZMUIAutoCompleteFilter<City> {
        private Disposable mDisposable;

        public CityFilter(LifecycleOwner lifecycleOwner, ZMUIAutoCompleteAdapter<City> adapter) {
            super(lifecycleOwner, adapter);
        }

        @Override
        public String converterString(City city) {
            return city.getName();
        }

        @Override
        protected void loadListDataSyn(@Nullable String filter, @NonNull List<City> Result) {
            mDisposable = Observable
                    .create((ObservableOnSubscribe<List<City>>) emitter -> {
                        List<City> citys = new ArrayList<>();
                        if (filter == null || filter.isEmpty()) {
                            citys.addAll(CITY_DATA_SET);
                        } else {
                            for (City item : CITY_DATA_SET) {
                                if (item.getName().contains(filter)) {
                                    citys.add(item);
                                }
                            }
                        }
                        emitter.onNext(citys);
                        emitter.onComplete();

                    })
                    .subscribe(list -> {
                        if (list != null && !list.isEmpty()) {
                            Result.addAll(list);
                        }
                    }, throwable -> {
                        throwable.printStackTrace();

                    });


        }

        @Override
        protected void cancelLoadList() {
            if (mDisposable != null && !mDisposable.isDisposed()) {
                mDisposable.dispose();
                mDisposable = null;
            }
        }
    }


    private static final List<City> CITY_DATA_SET = Arrays.asList(
            new City("北京市", "110000000000"),
            new City("天津市", "120000000000"),
            new City("河北省", "130000000000"),
            new City("山西省", "140000000000"),
            new City("内蒙古自治区", "150000000000"),
            new City("辽宁省", "210000000000"),
            new City("吉林省", "220000000000"),
            new City("黑龙江省", "230000000000"),
            new City("上海市", "310000000000"),
            new City("江苏省", "320000000000"),
            new City("浙江省", "330000000000"),
            new City("安徽省", "340000000000"),
            new City("福建省", "350000000000"),
            new City("江西省", "360000000000"),
            new City("山东省", "370000000000"),
            new City("河南省", "410000000000"),
            new City("湖北省", "420000000000"),
            new City("湖南省", "430000000000"),
            new City("广东省", "440000000000"),
            new City("广西壮族自治区", "450000000000"),
            new City("海南省", "460000000000"),
            new City("重庆市", "500000000000"),
            new City("四川省", "510000000000"),
            new City("贵州省", "520000000000"),
            new City("云南省", "530000000000"),
            new City("西藏自治区", "540000000000"),
            new City("陕西省", "610000000000"),
            new City("甘肃省", "620000000000"),
            new City("青海省", "630000000000"),
            new City("宁夏回族自治区", "640000000000"),
            new City("新疆维吾尔自治区", "650000000000")
    );


    public static final List<Dept> DEPT_DATA_SET = Arrays.asList(
            new Dept("001", "研发部"),
            new Dept("002", "商务部"),
            new Dept("003", "行政部"),
            new Dept("004", "后勤部"),
            new Dept("005", "运维部")
    );

}
